<?php

namespace app\company\controller;

use app\admin\model\product\Listo;
use app\admin\model\product\Lists;
use app\admin\model\product\Order;
use app\common\controller\CompanyApi;
use think\Request;

class Productorder extends CompanyApi
{
    protected static $model = null;
    protected $noNeedRight = '*';
    protected static $totalList = 15;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new Order();
    }

    /**
     * 今日盈利/亏损列表
     */
    public function lists()
    {
        $department_user_id = $this->request->post('department_user_id');
        $department_id = $this->request->post('department_id');
        $pageSize = input('page_size', self::$totalList);
        $type = input('type', 1);//1盈利，2损耗
        $today = strtotime('today');
        $where = [
            'updatetime' => [['>=', $today], ['<', $today + 86400]]
        ];

        $user = $this->auth->getUser();
        $user_ids = $this->getIds($user, $department_user_id, $department_id);
        if (isset($user_ids) && count($user_ids) > 0) {
            $where['department_user_id'] = ['in', $user_ids];
        }

        switch ($type) {
            case 1:
                //盈利
                $where['status'] = 20;
                break;
            case 2:
                //损耗
                $where['backinfo'] = 'fail';
                break;
        }

        $listso = self::$model->where($where)->select();
        $total = 0.00;
        $oids = [];
        foreach ($listso as $val) {
            if ($type == 1) {
                $total += round($val->total * $val->rate * 0.01, 2);
            } else {
                $total += round($val->transfee, 2);
            }

            $oids[] = $val->id;
        }

        $listss = [];
        if ($oids) {
            $map['order_id'] = ['in', $oids];
            $listss = Listo::field('id,tag,order_id,product_id,product_name,num,price,parameters,tool,pay_time,send_time,status')
                ->where($map)
                ->order('id desc')
                ->paginate($pageSize)->each(function ($item) {
                    $pInfo = Lists::where('id', $item['product_id'])->find();
//            $val['goodname'] = $pInfo['goodname'];
                    $val['cover_ico'] = make_full_url($pInfo['image']);
                    $val['intro'] = $pInfo['intro'];

                    $oInfo = Order::where('id', $item['order_id'])->find();
                    $item['realname'] = \app\admin\model\Transport::where('id', $oInfo->addressid)->value('realname');

                    $dInfo = \app\admin\model\department\User::field('department_id,username')->where('id', $oInfo->department_user_id)->find();
                    $item['username'] = $dInfo->username;

                    $item['name'] = \app\admin\model\department\Lists::where('id', $dInfo->department_id)->value('name');
                });
        }

        $this->success('订单列表', ['list' => $listss, 'total' => round($total, 2)]);
    }

    /**
     * 盈利排行榜
     */
    public function rankLists()
    {
//        $date = $this->request->post('date', $month);
//        $start = strtotime($date);

        $date_type = $this->request->post('date_type', 1);
        switch ($date_type) {
            case 1:
                $start = strtotime('today');
                break;
            case 2:
                $start = strtotime('Sunday -6 day', time());
                break;
        }

        $users = \app\admin\model\department\User::where('is_leader', 0)->where('status', 'normal')->select();
        $user_id = $this->auth->id;
        $where = [
            'updatetime' => ['>=', $start],
            'status' => 20
        ];

        $month = strtotime(date('Y-m-01', time()));
        $map = [
            'status' => 20,
            'send_time' => ['>=', $month]
        ];

        $data = [];
        $info = [];
        foreach ($users as $val) {
            $is_achievement = \app\admin\model\department\Lists::where('id', $val->department_id)->value('is_achievement');
            if ($is_achievement != 1) {
                continue;
            }
            $where['department_user_id'] = $val->id;
            $listso = self::$model->where($where)->select();
            $total = 0.00;
            $profile = 0.00;
            $num = 0;
            foreach ($listso as $v) {
                $total += $v->total;
                $profile += round($v->total * $v->rate * 0.01, 2);
                $num += 1;
            }

            $map['department_user_id'] = $val->id;
            $month_profile = Order::where($map)->sum('profile');
//            var_dump( Order::getLastSql());die;
            $department = \app\admin\model\department\Lists::where('id', $val->department_id)->value('name');
            $item = [
                'username' => $val->username,
                'nickname' => $val->nickname,
                'department' => $department,
                'num' => $num,
                'total' => $total,
                'profile' => $profile,
                'month_profile' => $month_profile
            ];

            $data[] = $item;

            if ($val->id == $user_id) {
                $info = $item;
                $info['rank'] = count($data);
            }
        }

        $profiles = array_column($data, 'profile');
        $month_profiles = array_column($data, 'month_profile');
        array_multisort($profiles, SORT_DESC, $month_profiles, SORT_DESC, $data);

        $this->success('排行榜', ['info' => $info, 'list' => $data]);
    }

    /**
     * 团队盈利
     */
    public function teamRanks()
    {
        $date_type = $this->request->post('date_type', 1);
        switch ($date_type) {
            case 1:
                $start = strtotime('today');
                break;
            case 2:
                $start = strtotime('Sunday -6 day', time());
                break;
        }
        $dlists = \app\admin\model\department\Lists::where('status', 'normal')->where('is_achievement', 1)->select();
        $where = [
            'status' => 20,
            'updatetime' => ['>=', $start]
        ];
        $user_id = $this->auth->id;
        $user = \app\admin\model\department\User::find($user_id);
        $team_data = [];
        foreach ($dlists as $v1) {
            $ids = \app\admin\model\department\User::where('department_id', $v1->id)->column('id');
            if ($ids) {
                $where['department_user_id'] = ['in', $ids];
                $total = Order::where($where)->sum('profile');
            } else {
                $total = 0.00;
            }

            $item = [
                'id' => $v1->id,
                'name' => $v1->name,
                'total' => round($total, 2)
            ];

            $team_data[] = $item;
        }

        $total_arr = array_column($team_data, 'total');
        array_multisort($total_arr, SORT_DESC, $team_data);
        $info = [];
        foreach ($team_data as $k2 => $v2) {
            if ($v2['id'] == $user->department_id) {
                $info = $v2;
                $info['rank'] = $k2 + 1;
            }
        }

        $this->success('团队盈利及排名', ['info' => $info, 'list' => $team_data]);
    }
}